﻿//==========================================================
//Đã sửa bởi tranninh_86_yb@yahoo.com ngày 15/07/2013
//==========================================================

using System;
using System.Data;
using System.Windows.Forms;
using System.Data.SqlClient;

using DevExpress.XtraEditors;
using prjKQXS_DLL;

namespace prjKQXS_UI
{
    public partial class frmDMchung : DevExpress.XtraEditors.XtraForm
    {
        private string tableName = "";
        private bool isPublic = true;

        public frmDMchung()
        {
            InitializeComponent();
        }

        public frmDMchung(string tableName)
        {
            InitializeComponent();
            this.tableName = tableName;
        }
        private void btnChon_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnThemmoi_Click(object sender, EventArgs e)
        {
            if (chkKhoiPhuc.Checked == true)
            {
                if (gvChung.RowCount > 0)
                {
                    clsCommon.deleteRestoreRow(tableName, "Ma_DT", gvChung.GetFocusedDataRow()["Ma_DT"].ToString(), true, false);
                    LoadData(isPublic);
                }
            }
            else
            {
                if (AddData(tableName) > 0)
                {
                    LoadData(isPublic);
                    gvChung.FocusedRowHandle = ((DataTable)gcChung.DataSource).Rows.Count - 1;
                }
            }
        }

        SqlConnection conn = new SqlConnection(BienTc.connString);
        private void frmDMCanBo_Load(object sender, EventArgs e)
        {
            if (tableName == "DMCHUCVU")
            {
                this.Text = "Danh mục chức vụ";
            }
            else if (tableName == "DMLOAIHOPDONG")
            {
                this.Text = "Danh mục loại hợp đồng";
            }
            else if (tableName == "DMLYDOTHANHLY")
            {
                this.Text = "Danh mục lý do thanh lý";
            }
            else
            {
                this.Text = "Danh mục tình trạng";
            }
            LoadData(isPublic);
        }

        public void LoadData(bool isPublic)
        {
            gcChung.DataSource = clsCommon.FillData("Select * from " + tableName + " Where isPublic='" + isPublic + "'");
        }

        private void gvChung_CellValueChanged(object sender, DevExpress.XtraGrid.Views.Base.CellValueChangedEventArgs e)
        {
            clsCommon.updateRowValue(tableName, e.Column.FieldName, "where ID=" + gvChung.GetFocusedDataRow()["ID"].ToString(), e.Value);
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            if (gvChung.RowCount > 0)
            {
                clsCommon.deleteRestoreRow(tableName, "Ma_DT", gvChung.GetFocusedDataRow()["Ma_DT"].ToString(), false, chkKhoiPhuc.Checked);
                LoadData(isPublic);
            }
        }

        private void chkKhoiPhuc_CheckedChanged(object sender, EventArgs e)
        {
            if (chkKhoiPhuc.Checked == true)
            {
                btnThemmoi.Text = "&Khôi phục";
                btnThemmoi.ImageIndex = 2;
                isPublic = false;
            }
            else
            {
                btnThemmoi.Text = "&Thêm mới";
                btnThemmoi.ImageIndex = 7;
                isPublic = true;
            }
            LoadData(isPublic);
        }

        public int AddData(string tableName)
        {
            if (conn.State == ConnectionState.Closed)conn.Open();
            string madt = clsCommon.laymadt(tableName, "Ma_DT", 5);
            SqlCommand cmd = new SqlCommand("Insert into " + tableName + "(Ma_DT,Ten_DT) values(@Ma_dt,@Ten_dt)", conn);
            cmd.Parameters.AddWithValue("@Ma_dt", madt);
            cmd.Parameters.AddWithValue("@Ten_dt", "Đối tượng thêm mới");
            return cmd.ExecuteNonQuery();
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            if (btnSearch.Tag.Equals("LOC"))
            {
                btnSearch.Tag = "BL";
                btnSearch.Text = "&Bỏ lọc";
                gvChung.OptionsView.ShowAutoFilterRow = true;
            }
            else
            {
                gvChung.OptionsView.ShowAutoFilterRow = false;
                btnSearch.Tag = "LOC";
                btnSearch.Text = "&Lọc";
            }
        }
    }
}